package com.jz.netthings.application.upload.service;

import com.github.tobato.fastdfs.domain.StorePath;
import com.github.tobato.fastdfs.proto.storage.DownloadByteArray;
import com.github.tobato.fastdfs.proto.storage.DownloadFileWriter;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import com.jz.netthings.appengine.config.FastProperties;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.channels.Pipe;

/**
 * @author lzf
 * @Date 2019/03/03 -22:24
 */
@Service
@Slf4j
@EnableConfigurationProperties(FastProperties.class)
public class UploadService {

    Logger logger = LoggerFactory.getLogger(getClass());
    @Autowired
    FastFileStorageClient storageClient;
    @Autowired
    FastProperties fastProperties;
    public  String uploadImage(MultipartFile file){
        try {
            String extension = StringUtils.substringAfterLast(file.getOriginalFilename(), ".");
            // 2.2、上传
            StorePath storePath = this.storageClient.uploadFile(
                    file.getInputStream(), file.getSize(), extension, null);
            // 2.3、返回完整路径
            return  fastProperties.getBaseUrl() + storePath.getFullPath();
        } catch (Exception e) {
            return null;
        }
    }
     public  String  uploadImageByBytes(byte[] bytes){
          try {
              InputStream inputStream = new ByteArrayInputStream(bytes);
              StorePath storePath = this.storageClient.uploadFile(
                      inputStream, bytes.length,"jpg", null);
              return storePath.getFullPath();
          }catch (Exception e){
              return null;
          }

     }
    /**
     * 下载文件通过fastdfs返回的路径
     * @param path
     * @return
     */
    public byte[] downloadImageReturnBytes(String path) {

        byte[] bytes = null;
        try{
            StorePath storePath = StorePath.praseFromUrl(path);
            DownloadByteArray downloadByteArray = new DownloadByteArray();
            bytes = storageClient.downloadFile(storePath.getGroup(), storePath.getPath(), downloadByteArray);
        }catch (Exception e){
            logger.error("下载文件通过fastdfs返回的路径异常："+ e.getMessage());
//            e.printStackTrace();
        }
        return  bytes;
    }

    /**
     * 下载文件到本地返回路径
     * @param path
     * @param fileName
     * @return
     */
     public  String downloadImageReturnPath(String path,String fileName){
         StorePath storePath = StorePath.praseFromUrl(path);
         DownloadFileWriter downloadFileWriter = new DownloadFileWriter(fileName);
         String s = storageClient.downloadFile(storePath.getGroup(), storePath.getPath(), downloadFileWriter);
      return s;
     }

    /**
     * 删除文件
     * @param path
     * @return
     */
    public  String deleteImage(String path){
        try {
            storageClient.deleteFile(path);
        } catch (Exception e) {
            return e.getMessage();
        }
        return "删除成功";
    }

}